<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Address;
use Illuminate\Http\Request;
use DB;
class AddressController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {

    }

    public function create(Request $request)
    {

        $address = Address::where('user_id', $user->id)->get();
        return view('order.create', [
            'address' => $address,

        ]);       

    }
    public function store(Request $request)
    {
        $this->validate($request, [
            'user_name'=>'required|string|max:255',
            'province'=>'required|string|max:255',
            'country'=>'required|string|max:255',
            'area'=>'required|string|max:255',
            'details'=>'required|string|max:255',
            //'user_id'=>'required|integer|max:10',
            'tell'=>'required|string|max:11',
            'code'=>'required|string|max:10',
        ], [
        ]);
        DB::beginTransaction();
        try {
            $address = Address::query()->create([
                'user_name' => $request['user_name'],
                'province' => $request['province'],
                'country' => $request['country'] ,
                'area' => $request['area'],
                'details' => $request['details'],
                'user_id' => $request->user()->id,
                'tell' => $request['tell'],
                'code' => $request['code'],
            ]);    
            DB::commit();
        } catch (\Throwable $throwable) {
            DB::rollBack();
            \Log::error($throwable);
            return response()->json(['status' => -1]);
        }
        return response()->json(['status' => 1]);
    }  
}

